fn parse_input() -> Vec<i32> {
    let input = std::fs::read_to_string("./day06/input.txt").unwrap();
    input.split(',').map(|line| line.parse().unwrap()).collect()
}

#[allow(unused)]
fn part1(mut numbers: Vec<i32>) {
    fn update(numbers: & mut Vec<i32>) {
        let mut count = 0;
        for number in numbers.iter_mut() {
            if (*number == 0) {
                *number = 6;
                count += 1;
            } else {
                *number -= 1;
            }
        }

        for i in 0..count {
            numbers.push(8);
        }
    }

    for _ in 0..80 {
        update(&mut numbers);
    }

    println!("Part1 {}", numbers.len());
}

#[allow(unused)]
fn part2(mut numbers: Vec<i32>) {

    fn fish_count (init_day: i32, total_day: i32) -> usize {
        if init_day >= total_day {
            return 1;
        }

        let born_count = (total_day - init_day - 1) / 7 + 1;

        let mut day = total_day - init_day - 1;
        let mut bady_sum = fish_count(8, day);

        for c in 1..born_count {
            day -= 7;
            bady_sum += fish_count(8, day);
        }
        
        bady_sum + 1
    }

    let result: usize = numbers.into_iter().map(|number| fish_count(number, 256)).sum();

    println!("Part2 {}", result);
}

fn main() {
    part1(parse_input());
    part2(parse_input());
}
